package com.sie.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sie.demo.entity.Menu;
import com.sie.demo.entity.vo.PageMenuVo;
import com.sie.demo.entity.vo.QueryMenuVo;
import com.sie.demo.mapper.MenuMapper;
import com.sie.demo.service.MenuService;
import com.sie.demo.util.MyDateConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author liaozhifeng
 * @date 2021/8/8 19:22
 * @Version 1.0
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Menu> implements MenuService {
    @Resource
    MenuMapper menuMapper;
    @Resource
    MyDateConverter myDateConverter;//自定义的字符串转为时间
    @Override
    public PageMenuVo selectAll(long currentPage, long pageSize, QueryMenuVo queryMenuVo) {
        //参数一是当前页，参数二是每页个数
        IPage<Menu> userPage = new Page<>(currentPage, pageSize);
        IPage<Menu> iPage;
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        // 没传条件来的时候直接分页根据创建时间排序查询所有的用户
        wrapper.orderByDesc("create_time");
//        wrapper.in("is_delete",0);
        //判断各参数是否有值，没有值，不管了；有值，把值加在wrapper中
        if (!StringUtils.isEmpty(queryMenuVo.getMenuName())) {
            wrapper.like("menu_name", queryMenuVo.getMenuName());
        }
        if (!StringUtils.isEmpty(queryMenuVo.getMenuType())) {
            wrapper.like("menu_type", queryMenuVo.getMenuType());
        }
        if (!StringUtils.isEmpty(queryMenuVo.getStatus())) {
            int status = Integer.valueOf(queryMenuVo.getStatus());
            wrapper.in("status", status);
        }
        iPage = menuMapper.selectPage(userPage, wrapper);
        List<Menu> list = iPage.getRecords();
//        根据父菜单的id得到父菜单的名字
        HashMap<Long,Menu> map =new HashMap<>();
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//        获取所有数据
        queryWrapper.in("is_delete",0);
        List<Menu> list1 = menuMapper.selectList(queryWrapper);
        for (Menu menu:list1) {
            map.put(menu.getMenuId(),menu);
        }
        for (Menu menu:list) {
           Menu parent = map.get(menu.getParentId());
           if (Objects.nonNull(parent)){
               menu.setParentName(parent.getMenuName());
           }
        }
        Long total = iPage.getTotal();
        PageMenuVo pageMenuVo = new PageMenuVo(list, total);
        return pageMenuVo;
    }

    @Override
    public Integer addMenu(Menu menu) {

        return  menuMapper.addMenu(menu);
    }

    @Override
    public Integer editMenu(Menu menu) {

        UpdateWrapper updateWrapper = new UpdateWrapper<Menu>();
        updateWrapper.in("menu_id",menu.getMenuId());
        return menuMapper.update(menu,updateWrapper);
    }

    @Override
    public List<Menu> selectAllMenu() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status",1);
        return menuMapper.selectList(queryWrapper);
    }

    @Override
    public List<Menu> handleMenu() {
//        得到所有菜单
        List<Menu> menuList = selectAllMenu();
//       存储一级菜单的集合
        List<Menu> menus = new ArrayList<>();
//        先找到所有的一级菜单
        for (int i = 0; i <menuList.size() ; i++) {
//            一级菜单的父菜单的id为0
            if (menuList.get(i).getParentId()==0){
                menus.add(menuList.get(i));
            }
        }
//        调用递归循环获取所有子菜单
        for (Menu menu:menus) {
            menu.setChildList(getChildList(menu.getMenuId(),menuList));
        }
        return menus;

    }

    @Override
    public Integer deleteMenuByIds(Integer menuId) {
        return menuMapper.deleteById(menuId);
    }

    /**
     * 递归子菜单
     * @param id
     * @param menus 所有菜单
     * @return
     */
     List<Menu> getChildList(Long id, List<Menu> menus) {
//        子菜单
        List<Menu> childMenus = new ArrayList<>();
        for (Menu menu:menus) {
            if (menu.getParentId().equals(id)){
                childMenus.add(menu);
            }
        }
        for (Menu menu : childMenus) {
            //数据库动态存储菜单的url,此时判断url为空的，则是节点菜单（存在子菜单）
//            if (StringUtils.isEmpty(menu.getUrl())) {
                // 递归调用
                menu.setChildList(getChildList(menu.getMenuId(), menus));
//            }
        }
        if (childMenus.size() == 0) {
            return null;
        }
        return childMenus;
    }


}
